<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="设备编号" prop="pileSn">
        <el-input
          v-model="queryParams.pileSn"
          placeholder="请输入设备编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>


    <el-table v-loading="loading" :data="pileList" @selection-change="handleSelectionChange">
      <el-table-column label="序号" type="index" align="center" width="70px"/>
      <el-table-column label="设备编号" align="center" prop="pileSn" />
      <el-table-column label="设备名称" align="center" prop="pileName" />
      <el-table-column label="设备型号" align="center" >
        <template #default="scope">
          <div v-for="item in pileModelList" :key="item.id">
            <span v-if="item.id == scope.row.modelId">{{item.modelName}}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="合作商" align="center" >
        <template #default="scope">
          <div v-for="item in partnerList" :key="item.id">
            <span v-if="item.id == scope.row.partnerId">{{item.partnerName}}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="详细地址" align="left" prop="addr" show-overflow-tooltip />
      <el-table-column label="运营状态" align="center" prop="deployStatus">
        <template #default="scope">
          <dict-tag :options="pile_status" :value="scope.row.deployStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="设备状态" align="center" prop="deviceStatus">
        <template #default="scope">
          <span v-if="scope.row.deployStatus != null">
            {{ JSON.parse(scope.row.deviceStatus).status==true?'正常':'异常' }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="getPileInfo(scope.row)" v-hasPermi="['manage:pile:query']">查看详情</el-button>
          <!-- <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['manage:pile:remove']">删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改电桩设备管理对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
    </el-dialog>
  </div>
</template>

<script setup name="Pile">
import { listPile, getPile, delPile, addPile, updatePile } from "@/api/manage/pile"
import { listPileModel } from "@/api/manage/pileModel"
import { listPartner } from "@/api/manage/partner"
import { listNode } from "@/api/manage/node"
import { listRegion } from "@/api/manage/region"
import { loadAllParams } from "@/api/page"
import { List } from "echarts"

const { proxy } = getCurrentInstance()
const { pile_status } = proxy.useDict('pile_status')

const pileList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    pileSn: null,
    pileName: null,
    modelId: null,
    regionId: null,
    nodeId: null,
    nodeType: null,
    partnerId: null,
    deployStatus: null,
    deviceStatus: null,
    strategyId: null,
  },
  rules: {
    modelId: [
      { required: true, message: "设备类型不能为空", trigger: "blur" }
    ],
    nodeId: [
      { required: true, message: "所属点位不能为空", trigger: "blur" }
    ],
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询电桩设备管理列表 */
function getList() {
  loading.value = true
  listPile(queryParams.value).then(response => {
    pileList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    pileSn: null,
    pileName: null,
    modelId: null,
    regionId: null,
    nodeId: null,
    nodeType: null,
    partnerId: null,
    addr: null,
    longitude: null,
    latitude: null,
    deployStatus: null,
    deviceStatus: null,
    clientId: null,
    strategyId: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    remark: null
  }
  proxy.resetForm("pileRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加电桩设备管理"
}

/** 查看详情操作 */
function getPileInfo(row) {
  reset()
  const _id = row.id || ids.value
  getPile(_id).then(response => {
    form.value = response.data
    open.value = true
    title.value = "设备详情"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["pileRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updatePile(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addPile(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal.confirm('是否确认删除电桩设备管理编号为"' + _ids + '"的数据项？').then(function() {
    return delPile(_ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('manage/pile/export', {
    ...queryParams.value
  }, `pile_${new Date().getTime()}.xlsx`)
}

/** 查询设备类型列表 */
const pileModelList = ref([])
function getPileModel() {
  listPileModel(loadAllParams).then(response => {
    pileModelList.value = response.rows
  })
}

/** 查询合作商列表 */
const partnerList = ref([])
function getParnter() {
  listPartner(loadAllParams).then(response => {
    partnerList.value = response.rows
  })
}

/** 查询点位列表 */
const nodeList = ref([])
function getNode() {
  listNode(loadAllParams).then(response => {
    nodeList.value = response.rows
  })
}

/** 查询区域列表 */ 
const regionList = ref([])
function getRegionList() {
  listRegion(loadAllParams.value).then(response => {
    regionList.value = response.rows;
  })
}

getRegionList()
getNode()
getPileModel()
getParnter()
getList()
</script>
